<template>
  <div class="app-container">
    <div class="main">
      <question-tree style="margin-left:0px;" @changeTree="changeTree" />
      <div style="flex:1;margin-left: 10px;">
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
          <el-form-item label="预案名称" prop="taskName">
            <el-input
              v-model="queryParams.taskName"
              placeholder="请输入预案名称"
              clearable
              @keyup.enter.native="handleQuery"
            />

            <!-- <p style="display:none">{{ treeNode.name }}</p> -->
          </el-form-item>
          <el-form-item label="预案状态" prop="taskState">
            <el-select v-model="queryParams.taskState" placeholder="请选择" clearable>
              <el-option
                v-for="dict in optionss"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="addTime">
            <el-date-picker
              v-model="queryParams.addTime"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择创建时间"
            />
          </el-form-item>
          <el-form-item label="预计结束时间" prop="endTime">
            <el-date-picker
              v-model="queryParams.endTime"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择预计结束时间"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['decision:task:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >创建预案</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['decision:task:edit']"
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['decision:task:remove']"
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['decision:task:export']"
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >调度员配置</el-button>
          </el-col>
          <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="taskId" />
          <el-table-column label="预案名称" align="center" prop="taskName" />
          <el-table-column label="问题描述" min-width="80" show-overflow-tooltip align="center" prop="problemDescription" />
          <el-table-column label="预案状态" align="center" prop="taskState">
            <template slot-scope="scope">
              {{ options[scope.row.taskState] }}
            </template>
          </el-table-column>
          <el-table-column label="生成时间" align="center" prop="addTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.taskState!=2"
                v-hasPermi="['decision:task:edit']"
                type="success"
                icon="el-icon-edit"
                @click="policyConfig(scope.row)"
              >策略配置</el-button>
              <el-button
                v-else
                v-hasPermi="['decision:task:remove']"
                type="primary"
                @click="details(scope.row)"
              >详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

    <!-- 添加或修改预案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="dialogDisable" label-width="80px">
        <el-form-item label="预案名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入预案名称" />
        </el-form-item>
        <el-form-item label="适合场景" prop="sceneId">
          <el-select v-model="form.sceneId" placeholder="请选择适合场景">
            <el-option
              v-for="dict in dict.type.rts_scene_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题时间" prop="problemTime">
          <el-date-picker
            v-model="form.problemTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择问题时间"
          />
        </el-form-item>
        <el-form-item label="问题地域" prop="problemArea">
          <el-input v-model="form.problemArea" placeholder="请输入问题地域" />
        </el-form-item>
        <el-form-item label="问题描述" prop="problemDescription">
          <el-input v-model="form.problemDescription" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="达到目标" prop="taskTarget">
          <el-input v-model="form.taskTarget" placeholder="请输入达到目标" />
        </el-form-item>
        <el-form-item label="预案状态" prop="taskState">
          <el-input v-model="form.taskState" placeholder="请输入预案状态" />
        </el-form-item>
        <el-form-item label="预案描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入预案描述" />
        </el-form-item>
        <el-form-item label="创建人" prop="addUser">
          <el-input v-model="form.addUser" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker
            v-model="form.addTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          />
        </el-form-item>
        <el-form-item label="预计结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计结束时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import questionTree from '@/components/createPlan/questionTree'
import { listTask, getTask, delTask, addTask, updateTask, lists } from '@/api/decision/task'
export default {
  name: 'Task',
  dicts: ['rts_scene_type'],
  components: {
    questionTree: questionTree
  },
  data() {
    return {
      props: {
        type: Object,
        default: () => {
          return {
            value: 'id', // ID字段名
            label: 'name', // 显示名称
            children: 'children' // 子级字段名
          }
        }
      },

      // 选项列表数据(树形结构的对象数组)
      options: { type: Array, default: [] },
      datas: [
        {
          id: 0,
          name: '所有',
          dictValue: '',
          children: [{
            sceneName: ''
          }]
        }
      ],

      options: ['未开始', '正在执行', '已完成'],
      optionss: [{
        value: 0,
        label: '未开始'
      }, {
        value: 1,
        label: '正在执行'
      },
      //  {
      //   value: 2,
      //   label: '已挂起'
      // }, 
      {
        value: 2,
        label: '已完成'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预案表格数据
      taskList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        sceneId: null,
        problemTime: null,
        problemArea: null,
        problemDescription: null,
        taskTarget: null,
        taskState: null,
        description: null,
        addUser: null,
        addTime: null,
        endTime: null,
        userId: ''
      },
      dialogDisable: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  computed: {
    userId() {
      return this.$store.state.user.userId
    }
  },

  created() {
    this.queryParams.userId = this.userId
    this.getList()
  },
  methods: {
    /** 查询预案列表 */
    getList() {
      // lists(null).then(response => {
      //   console.log(response.rows)
      //   this.options = response.rows
      // });

      this.loading = true
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    changeTree(val) {
      this.queryParams.taskName = val.name
      this.getData()
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        taskName: null,
        sceneId: null,
        problemTime: null,
        problemArea: null,
        problemDescription: null,
        taskTarget: null,
        taskState: null,
        description: null,
        addUser: null,
        addTime: null,
        endTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /**
     * 查看详情
     */
    details(row) {
      this.reset()
      const taskId = row.taskId || this.ids
      getTask(taskId).then(response => {
        this.form = response.data
        this.open = true
        this.dialogDisable = true
        this.title = '查看预案'
      })
    },
    /**
     * 策略配置
     */
    policyConfig(val) {
      localStorage.setItem('planParam', JSON.stringify(val))
      window.setTimeout(() => {
        this.$router.push({ path: '/decision/createPlan/2', query: { id: 3 }})
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$router.push('/decision/createPlan')
      // this.open = true;
      // this.title = "添加预案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const taskId = row.taskId || this.ids
      getTask(taskId).then(response => {
        this.form = response.data
        this.open = true
        this.dialogDisable = false
        this.title = '修改预案'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.taskId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids
      this.$modal.confirm('是否确认删除预案编号为"' + taskIds + '"的数据项？').then(function() {
        return delTask(taskIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('decision/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.main{
  display: flex;
}
</style>
